package com.oliet.controller;


import com.oliet.common.dto.dict.CreateDictionaryDTO;
import com.oliet.common.dto.dict.UpdateDictionaryDTO;
import com.oliet.common.result.CommonResponse;
import com.oliet.common.vo.dict.MainDictionaryVO;
import com.oliet.common.vo.page.PageVO;
import com.oliet.entity.Dictionary;
import com.oliet.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author JZP
 * @since 2023-12-30
 */
@RestController
@RequestMapping("/dict")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    @GetMapping("/getAll")
    public CommonResponse getAll(){
        List<MainDictionaryVO> result = this.dictionaryService.getAll();
        return CommonResponse.data(result);
    }


    @GetMapping("/list")
    public CommonResponse list(@RequestParam(value = "pageNo", defaultValue = "1") Long pageNo,
                               @RequestParam(value = "pageSize", defaultValue = "20") Long pageSize,
                               @RequestParam(value = "search", required = false) String search) {
        PageVO<Dictionary> result = this.dictionaryService.pageList(pageNo, pageSize,search);
        return CommonResponse.data(result);
    }

    @PostMapping("/create")
    public CommonResponse create(@Validated @RequestBody CreateDictionaryDTO param){
        this.dictionaryService.create(param);
        return CommonResponse.success();
    }

    @PutMapping("/update")
    public CommonResponse update(@Validated @RequestBody UpdateDictionaryDTO param){
        this.dictionaryService.update(param);
        return CommonResponse.success();
    }

    @GetMapping("/deleteById")
    public CommonResponse deleteById(@RequestParam Long id){
        this.dictionaryService.removeById(id);
        return CommonResponse.success();
    }

}
